package Cain.moveMenu 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 選單群組
	 * @author cain in liontravel 2010
	 */
	public class ItemGrup extends Sprite 
	{
		private var vec:ItemAce;
		private var v:Number;
		private var num:int;
		private var d:int;
		/**
		 * Item群組
		 * @param	_vec Item陣列
		 * @param	_d 距離
		 * @param	_v 每格速度
		 */
		public function ItemGrup(_vec:ItemAce,_d:int,_v:Number = 1) 
		{ 
			v = _v;
			vec = _vec;
			d = _d;
			num = vec.length;
			setup();
		}
		/**
		 * 移動開關
		 */
		public function set move(_swich:Boolean):void 
		{
			if (_swich) 
			{
				addEventListener(Event.ENTER_FRAME, onMove);
			}else 
			{
				removeEventListener(Event.ENTER_FRAME, onMove);
			}
		}
		
		private function onMove(e:Event):void 
		{
			for each (var _item:Item in vec) 
			{
				_item.x -= v;
				if (_item.x <= -d) 
				{
					if (ladt_item_x==-1) 
					{
						ladt_item_x = last_item.x;
					}
					_item.x = ladt_item_x + d;
				}
			}
		}
		private function setup():void
		{
			addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
		}
		private function onADDED_TO_STAGE(e:Event):void 
		{
			addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			init();
		}
		private var last_item:Item;
		private var ladt_item_x:int = -1;
		private function init():void
		{
			var _n:int = -1;
			for each (var _item:Item in vec) 
			{
				_item.x = _n * d;
				addChild(_item);
				last_item = _item;
				_n++;
			}
			addEventListeners();
		}
		
		public function addEventListeners():void
		{
			addEventListener(MouseEvent.ROLL_OVER, onROLL_OVER);
			addEventListener(MouseEvent.ROLL_OUT, onROLL_OUT);
			move = true;
		}
		
		private function onROLL_OUT(e:MouseEvent):void 
		{
			move = true;
		}
		
		private function onROLL_OVER(e:MouseEvent):void 
		{
			move = false;
		}
		private function removeEventListeners():void
		{
			removeEventListener(MouseEvent.ROLL_OVER, onROLL_OVER);
			removeEventListener(MouseEvent.ROLL_OUT, onROLL_OUT);
		}
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			removeEventListeners();
		}
	}
	
}